package com.crr.demo.impl;

import com.alibaba.fastjson.JSONObject;
import com.crr.demo.bean.User;
import com.crr.demo.impl.service.UserService;
import com.crr.demo.mapper.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserImpl implements UserService {
    @Resource
    UserMapper userMapper;
    public List<JSONObject> getAllUser() {
        List<User>users=userMapper.selectAllUser();
        List<JSONObject>list=new ArrayList<>();
        for(User user:users){
            list.add(user.getUserMsg());
        }
        return list;
    }
    public int addUser(User user){
        return userMapper.insertUser(user);
    }
     public String deleteUser(User user){
        try{
            userMapper.deleteUser(user);
            return "success";
        }catch (Exception e){
            return("error:\n"+e);
        }
    }
    public String recoverUser(User user){
        try{
            userMapper.recoverUser(user);
            return "success";
        }catch (Exception e){
            return("error:\n"+e);
        }
    }
    public String updateUser(User user){
         try{
            userMapper.updateUser(user);;
            return "success";
        }catch (Exception e){
            return("error:\n"+e);
        }
    }

    public void authUser(Long id, Long role) {
        userMapper.authUser(id, role);
    }

    public User getUserByLogin(String identityId, String pwd) {
        return userMapper.getUserByLogin(identityId, pwd);
    }


    public User getUser(String identityId) {
        return userMapper.getUser(identityId);
    }

    @Override
    public User getUserById(Long id) {
        return userMapper.getUserById(id);
    }


}
